﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kelper.Domain.Models;
using Kelper.Domain.Data.Abstract;
using Kelper.ViewModels;
using Kelper.Infrastructure.Helpers;

namespace Kelper.Controllers
{
    public class HomeController : BaseController
    {
        private IRecipeRepository recipes;
        private IIngredientRepository ingredients;
      
        public HomeController(IRecipeRepository recipeRepo, IIngredientRepository ingredientRepo)
        {
            recipes = recipeRepo;
            ingredients = ingredientRepo;
        }

        public ViewResult Index(int page = 1)
        {
            IEnumerable<Recipe> result = Paginate(
                recipes.Recipes.OrderByDescending(r => r.Created).Take(15), page
            );

            return View(new HomeViewModel
            {
                Recent = result,
                IngredientCloud = new IngredientCloud(ingredients.GetMostPopularWithCount(20)),
                CurrentPage = page,
                ItemsPerPage = PageSize,
                TotalItems = recipes.Recipes.Count()
            });
        }
    }
}
